//
//  main.m
//  02-block
//
//  Created by Jason on 15/9/25.
//  Copyright © 2015年 Jason. All rights reserved.
//

#import <Foundation/Foundation.h>
typedef int (^MyBlock)(int, int);
typedef int (^MyTestBlock)(int, int);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        /*
        int(^minusBlock)(int, int) = ^(int a, int b){
            
            return a - b;
        };
        
        minusBlock(5,3);
        
        int(^sumBlock)(int,int) = ^(int a, int b) {
          
            return a +b;
            
        };
        
        sumBlock(12, 11);
         
         */
        
        MyBlock minus = ^(int a, int b){
          
            return a - b;
            
        };
        
        
        MyTestBlock myTestBlock = ^(int a, int b) {
          
            return a + b ;
        };
        
    }
    return 0;
}

/*
 不能在block修改局部变量  static例外
 */
void test2() {
    int c = 10;
    
    static int a = 10;
    __block int b = 11;
    void(^block)() = ^{
        
        //            c = 12;  block内部不能修改默认的局部变量  加了static就可以
        
        a = 12;
        b = 12;
        NSLog(@"a = %d , b=%d",a, b);
        
        
    };
    
    block();

    
}

void test3() {
    
    // 没有参数 且没有返回值的block
    void(^myBlock)() = ^{
      
        int a = 10;
    
        NSLog(@"a is %d", a);
    };
    
    myBlock();
    
    
    
    // 返回值是int类型 带参数的 block
    int(^sumOperat)(int,int);
    sumOperat = ^(int v1, int v2) {
        
        return v1 + v2;
    };
    
    int(^sumOperat1)(int,int);
    sumOperat1 = ^(int a, int b){
      
        return a + b;
        
    };
    
    int(^sumOperat2)(int,int);
    sumOperat2 = ^(int a, int b) {
      
        return a + b;
    };
    
    int(^sumOperat3)(int,int);
    sumOperat3 = ^(int a, int b){
    
        return a + b;
        
    };
    
    int(^sumOperat4)(int,int);
    sumOperat4 = ^(int a, int b) {
      
        return a + b;
        
    };
    
    int(^sumOperat5)(int,int);
    sumOperat5 = ^(int a, int b) {
      
        return a + b;
    };
    
    
    int(^sumOperat6)(int,int);
    
    sumOperat6 = ^(int a, int b) {
      
        return a + b;
    };
    
    
    int(^sumOperat7)(int,int);
    
    sumOperat7 = ^(int a, int b) {
      
        return a + b;
        
    };
    
    
    
    
}

void test1() {
    /*
     1.左边的void:block没有返回值
     2.最右边的():没有参数
     3.中间的(^)block的标志,不能少
     
     */
    void (^myBlock)() = ^{
        
        int a = 10;
        int b = 11;
        NSLog(@"a = %d, b=%d",a, b);
        
    };
    
    myBlock();
    
    
    
    /*
     定义一个block变量,变量名是sumBlock
     最左边的int: block的返回值是int类型
     最右边的(int,int) block接收两个int类型的参数
     */
    
    int(^sumBlock)(int,int);
    
    sumBlock = ^(int v1, int v2){
        
        return v1 + v2;
    };
    
    int sum = sumBlock(10,20);
    
    NSLog(@"sum = %d", sum);
    

}
